878A - Short Program - CodeForces Solution


bitmasks constructive algorithms *1600

Please click on ads to support us..

Python Code:



def parse_command(l):
    op, val = l.split()
    return op, int(val)


def get_bit_command(commands, bit):
    no_ops = {
        ('&', 1),
        ('|', 0),
        ('^', 0)
    }
    num_xors = 0
    for op, val in reversed(commands):
        bit_val = (val >> bit) & 1
        if (op, bit_val) in no_ops:
            continue
        elif op == '&':
            return 'set', 0 ^ num_xors
        elif op == '|':
            return 'set', 1 ^ num_xors
        else:
            num_xors ^= 1
    else:
        return 'xor', num_xors


def main():
    n = int(input())
    commands = [parse_command(input()) for _ in range(n)]
    and_command = 0b11111_11111
    or_command = 0b00000_00000
    xor_command = 0b00000_00000
    for bit in range(10):
        bit_op, bit_val = get_bit_command(commands, bit)
        if bit_op == 'set':
            if bit_val == 0:
                and_command &= ~(1 << bit)
            else:
                or_command |= 1 << bit
        else:
            xor_command ^= bit_val << bit
    print(3)
    print('&', and_command)
    print('|', or_command)
    print('^', xor_command)


if __name__ == '__main__':
    main()

C++ Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        int a = 0, b = 1023;
        char p[3];
        int x;
        for (int i = 0; i < n; i++)
        {
            scanf("%s %d", p, &x);
            if (p[0] == '&')
            {
                a = a & x;
                b = b & x;
            }
            else if (p[0] == '|')
            {
                a = a | x;
                b = b | x;
            }
            else
            {
                a = a ^ x;
                b = b ^ x;
            }
        }
        int y1 = a & b;
        int y2 = a | b;
        int ans = 0;
        for (int i = 0; i < 10; i++)
        {
            if ((a >> i & 1) && !(b >> i & 1))
                ans |= 1 << i;
        }
        printf("3\n");
        printf("| %d\n", y1);
        printf("& %d\n", y2);
        printf("^ %d\n", ans);
    }
}


Comments

Submit
0 Comments
More Questions

1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps
1095B - Array Stabilization
291B - Command Line Arguments
1174B - Ehab Is an Odd Person
624B - Making a String
1064C - Oh Those Palindromes
1471A - Strange Partition
1746A - Maxmina
1746B - Rebellion
66C - Petya and File System
1746C - Permutation Operations
1199B - Water Lily
570B - Simple Game
599C - Day at the Beach
862A - Mahmoud and Ehab and the MEX
1525A - Potion-making
1744D - Divisibility by 2n
1744A - Number Replacement
1744C - Traffic Light
1744B - Even-Odd Increments
637B - Chat Order
546C - Soldier and Cards
18D - Seller Bob
842B - Gleb And Pizza
1746D - Paths on the Tree
1651E - Sum of Matchings
19A - World Football Cup
630P - Area of a Star